package train

import (
	"encoding/json"
	"gin-base/base/resp"
	"gin-base/common/middleware/token"
	"gin-base/common/models/train"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Index(this *gin.Context) {
	page, _ := strconv.Atoi(this.Query("page"))

	println(page)
	option := make(map[string]interface{})

	user := token.User(this)

	option["user_id"] = user.ID
	userList, pageInfo := train.GetRecords(option, page)

	resp.SetPages(this, pageInfo)
	resp.Success(this, userList)
}

type CreateForm struct {
	StartAt     string `form:"StartAt" json:"StartAt" binding:"required"`
	EndAt       string `form:"EndAt" json:"EndAt" binding:"required"`
	SportType   string `form:"SportType" json:"SportType" binding:"required"`
	Distance    string `form:"Distance" json:"Distance" binding:"required"`
	HeartRate   string `form:"HeartRate" json:"HeartRate" binding:"required"`
	Length      string `form:"Length" json:"Length" binding:"required"`
	Pace        string `form:"Pace" json:"Pace" binding:"required"`
	Calorie     string `form:"Calorie" json:"Calorie" binding:"required"`
	Speed       string `form:"Speed" json:"Speed" binding:"required"`
	IsTest      string `form:"IsTest" json:"IsTest" binding:"required"`
	Data        string `form:"Data" json:"Data" binding:"required"`
	RecordRange string `form:"RecordRange" json:"RecordRange" binding:"required"`
}

func Create(this *gin.Context) {
	var form CreateForm

	bindErr := this.BindJSON(&form)
	if bindErr == nil {
		user := token.User(this)

		var err error
		var record = new(train.Record)
		record.UserId = user.ID

		EndAt, err := strconv.ParseInt(form.EndAt, 10, 64)
		record.EndAt = time.Unix(EndAt, 0)

		StartAt, err := strconv.ParseInt(form.StartAt, 10, 64)
		record.StartAt = time.Unix(StartAt, 0)

		record.SportType, err = strconv.Atoi(form.SportType)
		record.Distance, err = strconv.ParseFloat(form.Distance, 64)
		record.HeartRate, err = strconv.Atoi(form.HeartRate)
		record.Length, err = strconv.Atoi(form.Length)
		record.Pace, err = strconv.Atoi(form.Pace)
		record.Calorie, err = strconv.Atoi(form.Calorie)
		record.Speed, err = strconv.ParseFloat(form.Speed, 64)
		record.Data = form.Data
		record.IsTest, err = strconv.Atoi(form.IsTest)
		record.Data = form.Data

		if err != nil {
			resp.Error(this, "PARAM", err.Error())
		}

		//入库保存
		if record.Save() {
			var postOfRange = make(map[string]string)
			rangStr := []byte(form.RecordRange)

			err = json.Unmarshal(rangStr, &postOfRange)
			if err != nil {
				resp.Error(this, "PARAM", err.Error())
			}

			var recordRange = new(train.RecordRange)
			recordRange.RecordId = record.ID
			recordRange.Restore, err = strconv.Atoi(postOfRange["Restore"])
			recordRange.Burning, err = strconv.Atoi(postOfRange["Burning"])
			recordRange.Cardiopulmonary, err = strconv.Atoi(postOfRange["Cardiopulmonary"])
			recordRange.Endurance, err = strconv.Atoi(postOfRange["Endurance"])
			recordRange.Anaerobic, err = strconv.Atoi(postOfRange["Anaerobic"])

			if err != nil {
				resp.Error(this, "PARAM", err.Error())
			}

			if recordRange.Save() {
				record.SetRecordRange()
			} else {
				resp.Error(this, "PARAM", "recordRange 数据保存失败")
			}

			resp.Success(this, record)
		} else {
			resp.Error(this, "PARAM", "数据保存失败")
		}
	} else {
		resp.Error(this, "PARAM", bindErr.Error())
	}
}

func View(this *gin.Context) {
	idstr := this.Param("id")

	id, err := strconv.ParseUint(idstr, 10, 32)
	if err != nil {
		resp.Error(this, "PARAM", "请传入有效Id")
	}

	record := train.GetOneRecordById(id)

	if record.ID == 0 {
		resp.Success(this, nil)
	} else {
		resp.Success(this, train.GetOneRecordById(id))
	}
}

func GetMonthData(this *gin.Context) {
	user := token.User(this)
	data := train.GetUserMonthInfo(user.ID)

	resp.Success(this, data)
}
